<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/9/27
 * Time: 14:29
 */

namespace app\common\model;


use think\console\input\Option;
use think\Model;

class QuestionOption extends Model
{
    public static $formateListArray;
    protected $table = 'ds_question_option';

    /**
     * 批量关联问题id
     * @param $ids  选项id，可以是数组，可以是以逗号分隔的字符串
     * @param $question_id
     * @return bool
     */
    public static function addQuestionId($ids, $question_id)
    {
        if (is_string($ids)) $ids = explode(',', $ids);
        $option = new QuestionOption();
        $res = $option->save(['question_id' => $question_id], function ($query) use($ids) {
            $query->where('id', 'in', $ids);
        });
        if ($res)
            return true;
        else
            return false;
    }

        /**
     * 批量关联问题id
     * @param $ids  选项id，可以是数组，可以是以逗号分隔的字符串
     * @param $question_id
     * @return bool
     */
    public static function addEquipmentId($ids, $question_id)
    {
        if (is_string($ids)) $ids = explode(',', $ids);
        // $option = new QuestionOption();
        $res = db('equipment_option')->where('id', 'in', $ids)->update(['equipment_id' => $question_id]);
        if ($res)
            return true;
        else
            return false;
    }

     /**
     * 批量关联问题id
     * @param $ids  选项id，可以是数组，可以是以逗号分隔的字符串
     * @param $question_id
     * @return bool
     */
    public static function addScienceId($ids, $question_id)
    {
        if (is_string($ids)) $ids = explode(',', $ids);
        // $option = new QuestionOption();
        $res = db('equipment_science')->where('id', 'in', $ids)->update(['equipment_id' => $question_id]);
        if ($res)
            return true;
        else
            return false;
    }


    public static function delOrAddOptions($diff_id)
    {
        if (is_string($diff_id)) $diff_id = explode(',', $diff_id);
        foreach ($diff_id as $key => $val) {
            $model = self::find($val);
            if ($model) {
                $model->delete();
            }

        }

    }

    /**
     * 获取给定的id获取选项列表，在通过格式化，将列表顺级组装
     * @param $ids
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOptionListByIds($ids)
    {
        if (is_string($ids)) $ids = explode(',', $ids);
        $options = QuestionOption::where('id', 'in', $ids)->order('id asc,order desc')->select();
        self::formateList($options);
        return self::$formateListArray;
    }

    public static function getOptionListByIdsNew($ids)
    {
        if (is_string($ids)) $ids = explode(',', $ids);
        $options = db('question_option')
            ->alias('q')
            ->field('q.id,q.content,q.parent_id,q.answer_id')
            ->where('q.id', 'in', $ids)
            ->order('q.id asc,q.order desc')
            ->select();

        return $options;
    }

    /**
     * 格式化答案列表。
     * @param $options
     * @param int $parent_id
     */
    public static function formateList($options, $parent_id = 0)
    {
        if (empty($options)) return '';
        foreach ($options as $key => $val) {
            if ($val->parent_id == $parent_id) {
                self::$formateListArray[$val->id] = $val;
                unset($options[$key]);
                self::formateList($options, $val->id);
            }
        }
    }

    /**
     * 选项关联答案
     * @param $data   key为otption的id，值为answer的id
     */
    public static function addAnswer($data)
    {
        if (empty($data)) return '';
        foreach ($data as $key => $val) {
            if ($key == 0) continue;
            $option = self::find($key);
            if (!$option) continue;
            $option->answer_id = trim($val, ',');
            $option->updated_at = date('Y-m-d H:i:s', time());
            $option->save();
        }
    }

    public function answers()
    {
        return $this->hasMany('Answer', 'option_id', 'id');
    }

    /**
     * 判断问题名称是否已经存在
     * @param $title
     *
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function uniqueOptionsByContent($title)
    {
        if (self::where('content', '=', $title)->find()) {
            return true;
        } else {
            return false;
        }
    }



}
